package com.imooc.mall.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.model.request.AddCategoryAdd;
import com.imooc.mall.model.request.UpdateCategoryUpdate;
import com.imooc.mall.model.vo.CategoryVO;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.websocket.Session;
import java.util.List;

/*分类对象的调用方法可以对这个类进行*/

@Controller
public class CategoryController {
    @Resource
    private UserService userService;


    @Resource
    private CategoryService categoryService;

    // @Valid 参数注解
    @ApiOperation("后台增加的按钮")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse createCategory(HttpSession session,@Valid @RequestBody AddCategoryAdd addCategoryAdd){
//        if (addCategoryAdd.getName() == null || addCategoryAdd.getType() == null || addCategoryAdd.getParentId() == null ||
//        addCategoryAdd.getOrderNum() == null){
//            return ApiRestResponse.error(ImoocMallExceptionEnum.PARA_NOT_NULL);
//        }

        User oldUser = (User) session.getAttribute(Constant.IM00C_MALL_USER);
        if(oldUser == null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }

        // //较验是不是管理员
        boolean b = userService.checkAdminRole(oldUser); //True 为管理呀
        if(b){
            // 是管理员执行操作//
            categoryService.add(addCategoryAdd);
            return ApiRestResponse.success();
        }else
        {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
//        if(oldUser.getRole() == 2){
//            User user = new User();
//
//        }
    }

    @ApiOperation("后台更新目录")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(@RequestBody @Valid UpdateCategoryUpdate updateCategoryUpdate,HttpSession session){
        User sessionAttribute = (User) session.getAttribute(Constant.IM00C_MALL_USER);
        if (sessionAttribute == null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        boolean b = userService.checkAdminRole(sessionAttribute);
        if (b){
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryUpdate,category);
            categoryService.UpdateCategory(category);
            return ApiRestResponse.success();
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台删除目录")
    @PostMapping("admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录列表")
    @GetMapping("admin/category/list")
    @ResponseBody
    public ApiRestResponse ListCategoryForAdmin(@RequestParam Integer pageNum ,@RequestParam Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台目录列表")
    @GetMapping("category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer(){
        List<CategoryVO> categoryVOS = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVOS);
    }
}
